$(function () {
    /**
     * 发送文本信息
     */
    $('#send').click(function () {
        var currentvisiid = Cookies.get('currentvisiid');
        if (!currentvisiid) {
            return false;
        }
        var txt = $.trim($('#text_in').html());
        var to_id = currentvisiid;
        var to_nick = $('#logue-item-' + currentvisiid).find('.c_name').text();
        var to_avatar = $('#logue-item-' + currentvisiid).find('.v-avatar').attr('src');
        var text = txt.replace(/\[.+?\]/g, function (a, b) {
            return "<img src='" + faces_lists[a] + "'/>";
        });
        text = text.replace(/[\r\n]/g, "").replace(/<div><br><\/div>/g, "");
        if (!text) return;
        console.log(text);
        playPrompt();
        // utype=say_to代表客服给游客聊天
        text = "" + text + "";/*客服消息记录*/
        var message = '{"data":"' + text.replace(/"/g, '\\"') + '","type":"say-text","utype":"say_to","from_id":"' + IM_user.from_id + '","from_nick":"' + IM_user.from_nick + '","from_avatar":"' + IM_user.from_avatar + '","to_id":"' + to_id + '","to_nick":"' + to_nick + '","to_avatar":"' + to_avatar + '"}';
        var pavatar = $('#pavatar-src').val();
        message = message.replace(/\n/g, "<br/>");
        var from_tpl = '<li class="chatmsg">' +
            '<div class="showtime"></div>' +
            '<div style="position: absolute;top: 26px;right: 2px;">' +
            '<img class="my-circle cu_pic" src="' + pavatar + '" width="50px" height="50px">' +
            '</div>' +
            '<div class="outer-right">' +
            '<div class="service">' +
            '<pre>' + text + '</pre>' +
            '</div>' +
            '</div>' +
            '</li>';
        $('.conversation').append(from_tpl);
        setTimeout(function () {
            $('#text_in').html("");
            $('#wrap').scrollTop($("#wrap")[0].scrollHeight);
        }, 10);
        WS.send(message);
        hook('admin_send', message, false);
    });

    /**
     * 上传图片
     */
    $("#upload_img input").change(function () {
        var currentvisiid = Cookies.get('currentvisiid');
        if (!currentvisiid) {
            return false;
        }
        var formData = new FormData();
        formData.append('ajaxupimg', $('input[name="upload_img"]').get(0).files[0]);
        formData.append('user_id', IM_user.from_id);
        formData.append('field', 'ajaxupimg');
        formData.append('user_type', 1);
        $('input[name="upload_img"]').val('');
        $.ajax({
            url: IM_toget_config.ajaxupimg,
            method: 'post',
            data: formData,
            dataType: 'json',
            processData: false,
            contentType: false,
            cache: false,
            async: false,
            success: function (res) {
                if (res.errno == 0) {
                    var to_id = currentvisiid;
                    var to_nick = $('#logue-item-' + currentvisiid).find('.c_name').text();
                    var to_avatar = $('#logue-item-' + currentvisiid).find('.v-avatar').attr('src');
                    var text = "<div class='mess-img'><img src='" + res.data[0] + "' /></div>";
                    var pavatar = $('#pavatar-src').val();
                    playPrompt();
                    // utype=say_to代表客服给游客聊天
                    var message = '{"data":"' + text + '","type":"say-img","utype":"say_to","from_id":"' + IM_user.from_id + '","from_nick":"' + IM_user.from_nick + '","from_avatar":"' + IM_user.from_avatar + '","to_id":"' + to_id + '","to_nick":"' + to_nick + '","to_avatar":"' + to_avatar + '"}';
                    var from_tpl = '<li class="chatmsg">' +
                        '<div class="showtime"></div>' +
                        '<div style="position: absolute;top: 26px;right: 2px;">' +
                        '<img class="my-circle cu_pic" src="' + pavatar + '" width="50px" height="50px">' +
                        '</div>' +
                        '<div class="outer-right">' +
                        '<div class="service">' +
                        '<pre>' + text + '</pre>' +
                        '</div>' +
                        '</div>' +
                        '</li>';
                    $('.conversation').append(from_tpl);
                    setTimeout(function () {
                        $('#wrap').scrollTop($("#wrap")[0].scrollHeight);
                    }, 10);
                    WS.send(message);
                } else {
                    we_im_msg(res.msg);
                }
            }
        });
    });

    /**
     * 上传文件
     */
    $("#upload_file input").change(function () {
        var currentvisiid = Cookies.get('currentvisiid');
        if (!currentvisiid) {
            return false;
        }
        var formData = new FormData();
        formData.append('ajaxupfile', $('input[name="upload_file"]').get(0).files[0]);
        formData.append('user_id', IM_user.from_id);
        formData.append('field', 'ajaxupfile');
        formData.append('user_type', 1);
        $('input[name="upload_file"]').val('');
        $.ajax({
            url: IM_config.ajaxupfile,
            method: 'post',
            data: formData,
            dataType: 'json',
            processData: false,
            contentType: false,
            cache: false,
            async: false,
            success: function (res) {
                if (res.errno == 0) {
                    var to_id = currentvisiid;
                    var to_nick = $('#logue-item-' + currentvisiid).find('.c_name').text();
                    var to_avatar = $('#logue-item-' + currentvisiid).find('.v-avatar').attr('src');
                    var text = "<div class='mess-file'><a href='" + res.data[0] + "' target='_blank'><i class='iconfont icon-78'></i><br>查看文件</a></div>";
                    playPrompt();
                    var pavatar = $('#pavatar-src').val();
                    // utype=say_to代表客服给游客聊天
                    var message = '{"data":"' + text + '","type":"say-file","utype":"say_to","from_id":"' + IM_user.from_id + '","from_nick":"' + IM_user.from_nick + '","from_avatar":"' + IM_user.from_avatar + '","to_id":"' + to_id + '","to_nick":"' + to_nick + '","to_avatar":"' + to_avatar + '"}';
                    var from_tpl = '<li class="chatmsg">' +
                        '<div class="showtime"></div>' +
                        '<div style="position: absolute;top: 26px;right: 2px;">' +
                        '<img class="my-circle cu_pic" src="' + pavatar + '" width="50px" height="50px">' +
                        '</div>' +
                        '<div class="outer-right">' +
                        '<div class="service">' +
                        '<pre>' + text + '</pre>' +
                        '</div>' +
                        '</div>' +
                        '</li>';
                    $('.conversation').append(from_tpl);
                    setTimeout(function () {
                        $('#wrap').scrollTop($("#wrap")[0].scrollHeight);
                    }, 10);
                    WS.send(message);
                } else {
                    we_im_msg(res.msg);
                }
            }
        });
    });
});

/**
 * 更新客服聊天信息
 */
function we_im_update_current_right_message(data) {
    var from_tpl = '<li class="chatmsg">' +
        '<div class="showtime"></div>' +
        '<div style="position: absolute;top: 26px;right: 2px;">' +
        '<img class="my-circle cu_pic" src="' + data.receive_avatar + '" width="50px" height="50px">' +
        '</div>' +
        '<div class="outer-right">' +
        '<div class="service">' +
        '<pre>' + data.content + '</pre>' +
        '</div>' +
        '</div>' +
        '</li>';
    $('.conversation').append(from_tpl);
    setTimeout(function () {
        $('#wrap').scrollTop($("#wrap")[0].scrollHeight);
    }, 10);
}